const db = wx.cloud.database()
// pages/activity/activity.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "card_data":[],
    card_id:[],
    showView: false,
  },

  onChangeShowState: function () {
    var that = this;
    that.setData({
      showView: (!that.data.showView1)
    })
  },

  returnJump:function(e){
    wx.navigateBack({ changed: true });
  },

  collection:function(e){
    var that = this
    if (getApp().globalData.openid == null) {
      that.getUserProfile()
    } 
    else {
      if (that.data.showView == false) {
        // 等待窗口
      wx.showToast({
        title: 'Loading……',
        icon:"loading",
        duration:390000
      })
      db.collection('collection_data').add({
        // data 字段表示需新增的 JSON 数据
        data: {
          collection_data: that.data.card_data
        },
        success: function (res) {
          // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
          console.log(res)
          wx.showToast({
            title: '收藏成功',
            icon: 'success',
            duration: 2000
          })
        }
      })
      that.setData({
        showView: true
      })
      }
      else {
        db.collection('collection_data').doc(that.data.card_id[0]).remove({
          success: function () {
            wx.showToast({
              title: '删除成功',
              duration:2000
            })
            
          }
        })
        that.setData({
          showView: false
        })
      }

    }
    
  },
  
  getUserProfile(e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', 
      success: (res) => {
        console.log(res)
        getApp().globalData.userInfo = res.userInfo
        this.getopenid()
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 2000
        })
      }
    })

  },
  getopenid() {
    var that = this
    wx.cloud.callFunction({
      name: "getopenid",
      success(res) {
        console.log("获取openid成功", res.result.openid)
        // console.log(res)
        that.setData({
          openid: res.result.openid
        })
        console.log()
        getApp().globalData.openid = that.data.openid
      },
      fail(res) {
        console.log("获取openid成功", res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;
    db.collection("collection_data").get().then(res=>{
      console.log("成功1",res)
      var card_id=[];
      var _openid= getApp().globalData.openid;
      for(let i = 0;i < res.data.length;i++){
        console.log("空",res.data[i]._openid)
        if (res.data[i]._openid == _openid){
          // collection_data.push(res.data[i])
          console.log("获取标题",res.data[i].collection_data.title)
          console.log("该页面的标题",that.data.card_data.title)
          
          var title = res.data[i].collection_data.title
          if (that.data.card_data.title == title){
            console.log("该页面的id",res.data[i]._id)
            card_id.push(res.data[i]._id)
            that.onChangeShowState()
          }
        }
      }
      console.log("该页面的id",card_id)
      that.setData({
        card_id,
      })
      
    })
  },

  onShow: function () {
    console.log("collection=", getApp().globalData.searched)
    this.setData({
      card_data: getApp().globalData.searched,
    })

  },
})